package com.aoxin.controller.client;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.CourseDto;
import com.aoxin.model.dto.SchoolCourseDto;
import com.aoxin.service.inter.course.SchoolCourseDtoService;
import com.aoxin.service.inter.course.systemCourse.SyllabusService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.PageInfo;
import com.aoxin.util.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;

@Controller
@ResponseBody
@RequestMapping("/web/course")
public class CourseController {

	@Autowired
	private SyllabusService syllabusService;

	@Autowired
	private SchoolCourseDtoService schoolCourseDtoService;

	/**
	 * 根据孩子所在的学校查询课程
	 * 
	 */
	@RequestMapping("/getSyllabusListBySchool")
	public AjaxRes getSyllabusListBySchool(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, @RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			PageHelper.startPage(pageIndex, pageSize);
			Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
			map.put("childrenId", childrenId);
			List<CourseDto> courseDtos = this.syllabusService.getSyllabusListByChildren(map);
			PageInfo<CourseDto> pageInfo = new PageInfo<CourseDto>(courseDtos);
			ajaxRes.setSucceed(pageInfo, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据孩子所在的学校查询课程
	 * 
	 */
	@RequestMapping("/getSyllabusBySchool")
	public AjaxRes getSyllabusBySchool(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			Object object = map.get("childrenId");
			if (null == object) {
				Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
				map.put("childrenId", childrenId);
			}
			CourseDto courseDto = this.syllabusService.getSyllabusByChildren(map);
			ajaxRes.setSucceed(courseDto, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据孩子所在的学校查询课程
	 * 
	 */
	@RequestMapping("/purchaseCourse")
	public AjaxRes purchaseCourse(AjaxRes ajaxRes) {
		try {
			// TODO
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询学校下所有课程课程包
	 * 
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/getSchoolAllCourse")
	public AjaxRes getSchoolAllCourse(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, AjaxRes ajaxRes) {
		Integer schoolId = ThreadLocalUtil.getChildren().getSchoolId();
		if (schoolId != null) {
			PageHelper.startPage(pageIndex, pageSize);
			List<SchoolCourseDto> schoolCourseDtoList = schoolCourseDtoService.getSchoolCourseDtoList(schoolId,null);
			PageInfo<SchoolCourseDto> pageInfo = new PageInfo<SchoolCourseDto>(schoolCourseDtoList);
			ajaxRes.setSucceed(pageInfo, Const.DATA_SUCCEED);
		}
		return ajaxRes;
	}

	/**
	 * 查询学校下所有课程包
	 * 
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/getSchoolAllCoursePackage")
	public AjaxRes getSchoolAllCoursePackage(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, AjaxRes ajaxRes) {
		Integer schoolId = ThreadLocalUtil.getChildren().getSchoolId();
		if (schoolId != null) {
			PageHelper.startPage(pageIndex, pageSize);
			List<SchoolCourseDto> schoolCourseDtoList = schoolCourseDtoService.getSchoolCourseDtoList(schoolId,1);
			PageInfo<SchoolCourseDto> pageInfo = new PageInfo<SchoolCourseDto>(schoolCourseDtoList);
			ajaxRes.setSucceed(pageInfo, Const.DATA_SUCCEED);
		}
		return ajaxRes;
	}	
	
	
	/**
	 * 查询学校下所有课程课程包
	 * 
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/getSchoolCourse")
	public AjaxRes getSchoolCourse(@RequestParam("courseId") Integer courseId, AjaxRes ajaxRes) {
		Integer schoolId = ThreadLocalUtil.getChildren().getSchoolId();
		if (schoolId != null) {
			SchoolCourseDto schoolCourseDto = schoolCourseDtoService.getSchoolCourseDto(schoolId, courseId);
			ajaxRes.setSucceed(schoolCourseDto, Const.DATA_SUCCEED);
		} else
			ajaxRes.setFailMsg(Const.DATA_FAIL);
		return ajaxRes;
	}
	
}
